<template>
	<view class="service-manage-container">
		<view class="header">
			<view class="title">我发布的需求</view>
			<view class="tabs">
				<view class="tab-item" v-for="(tab, index) in tabs" :key="index"
					:class="{ active: currentTab === index }" @click="changeTab(index)">
					<text>{{ tab.name }}</text>
					<!-- <view class="badge" v-if="tab.count > 0">{{ tab.count }}</view> -->
				</view>
			</view>
		</view>

		<!-- 空状态 -->
		<view class="empty-state" v-if="serviceList.length === 0">
			<image class="empty-image" src="/static/images/empty.png" mode="aspectFit"></image>
			<text class="empty-text">暂无{{ tabs[currentTab].name }}服务</text>
			<view class="empty-btn" @click="goToPublish">去发布服务</view>
		</view>

		<!-- 服务列表 -->
		<view class="service-list" v-else>
			<view class="service-item" v-for="(item, index) in serviceList" :key="index">
				<view class="service-header">
					<view class="service-info">
						<!-- <image class="service-avatar" :src="item.avatar" mode="aspectFill"></image> -->
						<view class="service-meta">
							<!-- <text class="service-name">{{ item.name }}</text> -->
							<text class="service-time ml-10">发布时间：{{ item.createTime }}</text>
						</view>
					</view>
					<view class="service-status" :class="{
						'status-pending': item.status === 2,
						'status-approved': item.status === 1,
						'status-rejected': item.status === 3
					}">{{ getStatusText(item.status) }}</view>
				</view>

				<view class="service-content" @click="viewServiceDetail(item)">
					<image class="service-cover" :src="item.coverImage" mode="aspectFill"></image>
					<view class="service-brief">
						<text class="service-title">{{ item.serviceName }}</text>
						<text class="service-desc">{{ item.content }}</text>
						<view class="service-stats">
							<view class="stat-item">
								<text class="iconfont icon-liulan"></text>
								<text>{{ item.viewCount || 0 }}</text>
							</view>
							<view class="stat-item">
								<text class="iconfont icon-dianzan1"></text>
								<text>{{ item.likeCount || 0 }}</text>
							</view>

						</view>
					</view>
				</view>

				<view class="service-actions">
					<view class="action-item" @click="refreshService(item)"
						v-if="item.status === 1 || item.status === 3">
						<text class="iconfont icon-shuaxin"></text>
						<text>{{ item.status === 3 ? '重新提交' : '刷新' }}</text>
					</view>
					<view class="action-item" @click="editService(item)" v-if="item.status !== 2">
						<text class="iconfont icon-bianji " style="font-size: 36rpx;"></text>
						<text>编辑</text>
					</view>
					<!-- <view class="action-item" @click="toggleService(item)" v-if="item.status === 1">
						<text class="iconfont-yjgs fs-40">&#xe60c</text>
						<text>下架</text>
					</view> -->
					<view class="action-item" @click="deleteService(item)">
						<text class="iconfont icon-shanchu"></text>
						<text>删除</text>
					</view>
				</view>

				<!-- 审核失败原因 -->
				<view class="reject-reason" v-if="item.status === 3">
					<text class="reason-title">驳回原因：</text>
					<text class="reason-content">{{ item.rejectReason || '内容不符合规范，请修改后重新提交' }}</text>
				</view>
			</view>
		</view>

		<!-- 加载更多 -->
		<view class="load-more" v-if="serviceList.length > 0">
			<uni-load-more :status="loadMoreStatus"></uni-load-more>
		</view>

		<!-- 底部按钮 -->
		<view class="float-btn" @click="goToPublish">
			<text class="iconfont icon-add"></text>
			<text>发布服务</text>
		</view>

		<!-- 确认弹窗 -->
		<view class="modal" v-if="showModal">
			<view class="modal-content">
				<view class="modal-title">{{ modalInfo.title }}</view>
				<view class="modal-message">{{ modalInfo.message }}</view>
				<view class="modal-btns">
					<view class="modal-btn cancel" @click="closeModal">取消</view>
					<view class="modal-btn confirm" @click="confirmModal">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		findServiceGetUserList,
		findServiceDelete
	} from "@/api/extend.js"
	export default {
		data() {
			return {
				options: {},
				currentTab: 0,
				tabs: [{
						name: '全部',
						count: 0,
						status: 4
					},
					{
						name: '已通过',
						count: 0,
						status: 1
					},
					{
						name: '审核中',
						count: 0,
						status: 2
					},
					{
						name: '未通过',
						count: 0,
						status: 3
					}
				],
				serviceList: [],
				page: 1,
				limit: 10,
				hasMore: true,
				isLoading: false,
				showModal: false,
				modalInfo: {
					title: '',
					message: '',
					callback: null
				},
				currentService: null
			}
		},
		computed: {
			loadMoreStatus() {
				if (this.isLoading) return 'loading';
				if (!this.hasMore) return 'noMore';
				return 'more';
			}
		},
		onLoad(options) {
			this.options = options;
			this.findServiceGetUserListFun();
			// this.loadServiceList();
		},
		onReachBottom() {
			if (this.hasMore && !this.isLoading) {
				this.isLoading = true;
				this.page++;
				this.findServiceGetUserListFun();
			}
		},
		onPullDownRefresh() {
			this.page = 1;
			this.serviceList = [];
			this.findServiceGetUserListFun();
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			findServiceGetUserListFun() {
				findServiceGetUserList({
					type: this.options.type,
					status: this.tabs[this.currentTab].status,
					limit: this.limit,
					page: this.page
				}).then(res => {
					let newList = [];
					if (res.data && res.data.data) {
						newList = res.data.data.map(item => {
							return {
								id: item.id,
								createTime: this.$timer.formatTime(item.create_time),
								serviceName: item.title,
								content: item.content,
								coverImage: this.$timer.ensureUrl(item.cover_image),
								status: item.status, // 接口定义：1已通过 2审核中 3未通过 4全部
								viewCount: item.see_number || 0,
								likeCount: item.like_number || 0,
								isOnline: item.is_online === 1,
								avatar: item.avatar || '/static/images/avatar.png',
								name: item.name || '用户',
								rejectReason: item.remark || ''
							}
						});
					}

					// 如果是第一页，直接替换列表
					if (this.page === 1) {
						this.serviceList = newList;
					} else {
						// 否则追加到现有列表
						this.serviceList = [...this.serviceList, ...newList];
					}

					// 判断是否还有更多数据
					if (res.data && res.data.page_count !== undefined) {
						this.hasMore = this.page < res.data.page_count;
					} else {
						// 否则使用返回数据长度判断
						this.hasMore = newList.length >= this.limit;
					}
					this.isLoading = false;
				}).catch(err => {
					console.error('加载服务列表失败', err);
					this.isLoading = false;
					this.hasMore = false; // 出错时设置没有更多数据
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					});
				});
			},

			// 切换选项卡
			changeTab(index) {
				if (this.currentTab !== index) {
					this.currentTab = index;
					this.page = 1;
					this.serviceList = [];
					this.findServiceGetUserListFun();
				}
			},

			// 获取状态文本
			getStatusText(status) {
				switch (status) {
					case 1:
						return '已通过';
					case 2:
						return '审核中';
					case 3:
						return '未通过';
					default:
						return '未知状态';
				}
			},

			// 查看服务详情
			viewServiceDetail(item) {
				// 跳转到服务详情页
				uni.navigateTo({
					url: '/pages/extend/index/index_serve_desc?id=' + item.id
				});
			},

			// 刷新服务/重新提交
			refreshService(item) {
				if (item.status === 3) {
					// 重新提交审核
					this.editService(item);
				} else {
					// 刷新服务信息
					this.page = 1;
					this.findServiceGetUserListFun();
					uni.showToast({
						title: '刷新成功',
						icon: 'success'
					});
				}
			},

			// 编辑服务
			editService(item) {
				uni.navigateTo({
					url: '/pages/extend/index/index_serve_send?id=' + item.id + '&mode=edit&type=' + this.options.type
				});
			},

			// 上/下架服务
			toggleService(item) {
				this.currentService = item;
				this.modalInfo = {
					title: item.isOnline ? '下架服务' : '上架服务',
					message: item.isOnline ? '确定要下架该服务吗？' : '确定要上架该服务吗？',
					callback: this.confirmToggleService
				};
				this.showModal = true;
			},

			// 确认上/下架服务
			confirmToggleService() {
				const item = this.currentService;
				item.isOnline = !item.isOnline;

				uni.showToast({
					title: item.isOnline ? '上架成功' : '下架成功',
					icon: 'success'
				});
				this.closeModal();
			},

			// 删除服务
			deleteService(item) {
				this.currentService = item;
				this.modalInfo = {
					title: '删除服务',
					message: '确定要删除该服务吗？删除后无法恢复',
					callback: this.confirmDeleteService
				};
				this.showModal = true;
			},

			// 确认删除服务
			confirmDeleteService() {
				findServiceDelete({
					id: this.currentService.id
				}).then(res => {
					if (res.status === 200) {
						const index = this.serviceList.findIndex(item => item.id === this.currentService.id);
						if (index !== -1) {
							this.serviceList.splice(index, 1);
						}
						uni.showToast({
							title: '删除成功',
							icon: 'success'
						});
					} else {
						uni.showToast({
							title: res.msg || '删除失败',
							icon: 'none'
						});
					}
					this.closeModal();
				}).catch(err => {
					console.error('删除服务失败', err);
					uni.showToast({
						title: '删除失败',
						icon: 'none'
					});
					this.closeModal();
				});
			},

			// 关闭弹窗
			closeModal() {
				this.showModal = false;
			},

			// 确认弹窗操作
			confirmModal() {
				if (typeof this.modalInfo.callback === 'function') {
					this.modalInfo.callback();
				}
				this.closeModal();
			},

			// 前往发布服务页面
			goToPublish() {
				uni.navigateTo({
					url: '/pages/extend/index/index_serve_send?type=' + this.options.type
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.service-manage-container {
		min-height: 100vh;
		background-color: #f8f9fd;
		padding-bottom: 120rpx;
	}

	.header {
		background-color: #fff;
		padding: 30rpx;
		box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

		.title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 30rpx;
		}

		.tabs {
			display: flex;
			border-bottom: 2rpx solid #f0f2f5;

			.tab-item {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 20rpx 0;
				font-size: 28rpx;
				color: #606266;
				position: relative;

				&.active {
					color: #1890ff;
					font-weight: 500;

					&::after {
						content: '';
						position: absolute;
						bottom: -2rpx;
						left: 20%;
						width: 60%;
						height: 6rpx;
						background: #1890ff;
						border-radius: 6rpx;
					}
				}

				.badge {
					background-color: #ff4d4f;
					color: #fff;
					font-size: 20rpx;
					height: 30rpx;
					min-width: 30rpx;
					border-radius: 15rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					padding: 0 8rpx;
					margin-left: 8rpx;
				}
			}
		}
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;

		.empty-image {
			width: 240rpx;
			height: 240rpx;
			margin-bottom: 30rpx;
		}

		.empty-text {
			font-size: 28rpx;
			color: #909399;
			margin-bottom: 40rpx;
		}

		.empty-btn {
			background: #1890ff;
			color: #fff;
			font-size: 28rpx;
			padding: 16rpx 40rpx;
			border-radius: 40rpx;
		}
	}

	.service-list {
		padding: 20rpx;
	}

	.service-item {
		background-color: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
		box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

		.service-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;
			border-bottom: 2rpx solid #f5f7fa;

			.service-info {
				display: flex;
				align-items: center;

				.service-avatar {
					width: 60rpx;
					height: 60rpx;
					border-radius: 30rpx;
					margin-right: 16rpx;
				}

				.service-meta {
					display: flex;
					flex-direction: column;

					.service-name {
						font-size: 28rpx;
						color: #333;
						font-weight: 500;
					}

					.service-time {
						font-size: 24rpx;
						color: #909399;
						margin-top: 4rpx;
						// margin-left: -60rpx;
						/* 向左偏移 */
					}
				}
			}

			.service-status {
				font-size: 24rpx;
				padding: 6rpx 20rpx;
				border-radius: 30rpx;

				&.status-pending {
					background-color: #e6f7ff;
					color: #1890ff;
				}

				&.status-approved {
					background-color: #f6ffed;
					color: #52c41a;
				}

				&.status-rejected {
					background-color: #fff2f0;
					color: #ff4d4f;
				}
			}
		}

		.service-content {
			display: flex;
			padding: 20rpx;

			.service-cover {
				width: 180rpx;
				height: 180rpx;
				border-radius: 12rpx;
				margin-right: 20rpx;
				background-color: #f5f7fa;
			}

			.service-brief {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.service-title {
					font-size: 30rpx;
					color: #333;
					font-weight: 500;
					margin-bottom: 10rpx;
				}

				.service-desc {
					font-size: 26rpx;
					color: #606266;
					line-height: 1.5;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
					flex: 1;
				}

				.service-stats {
					display: flex;
					margin-top: 16rpx;

					.stat-item {
						display: flex;
						align-items: center;
						margin-right: 30rpx;
						font-size: 24rpx;
						color: #909399;

						.iconfont {
							margin-right: 6rpx;
							font-size: 26rpx;
						}
					}
				}
			}
		}

		.service-actions {
			display: flex;
			border-top: 2rpx solid #f5f7fa;

			.action-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 16rpx 0;
				position: relative;

				&::after {
					content: '';
					position: absolute;
					right: 0;
					top: 20%;
					height: 60%;
					width: 2rpx;
					background-color: #f5f7fa;
				}

				&:last-child::after {
					display: none;
				}

				.iconfont {
					font-size: 36rpx;
					color: #606266;
					margin-bottom: 6rpx;
				}

				text:last-child {
					font-size: 24rpx;
					color: #606266;
				}
			}
		}

		.reject-reason {
			background-color: #fff8f8;
			padding: 16rpx 20rpx;
			border-top: 2rpx solid #f5f7fa;

			.reason-title {
				font-size: 24rpx;
				color: #ff4d4f;
				font-weight: 500;
			}

			.reason-content {
				font-size: 24rpx;
				color: #606266;
			}
		}
	}

	.load-more {
		text-align: center;
		padding: 30rpx 0;
		font-size: 26rpx;
		color: #909399;
	}

	.float-btn {
		position: fixed;
		right: 40rpx;
		bottom: 60rpx;
		background: linear-gradient(to right, #1890ff, #0050b3);
		color: #fff;
		border-radius: 45rpx;
		display: flex;
		align-items: center;
		padding: 20rpx 40rpx;
		box-shadow: 0 10rpx 20rpx rgba(24, 144, 255, 0.3);
		z-index: 10;

		.iconfont {
			font-size: 32rpx;
			margin-right: 10rpx;
		}

		text {
			font-size: 28rpx;
		}
	}

	.modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 100;

		.modal-content {
			width: 560rpx;
			background-color: #fff;
			border-radius: 16rpx;
			overflow: hidden;

			.modal-title {
				font-size: 32rpx;
				color: #333;
				font-weight: 500;
				padding: 30rpx;
				text-align: center;
				border-bottom: 2rpx solid #f0f0f0;
			}

			.modal-message {
				font-size: 28rpx;
				color: #606266;
				padding: 40rpx 30rpx;
				text-align: center;
				line-height: 1.5;
			}

			.modal-btns {
				display: flex;
				border-top: 2rpx solid #f0f0f0;

				.modal-btn {
					flex: 1;
					height: 90rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28rpx;

					&.cancel {
						color: #606266;
						border-right: 2rpx solid #f0f0f0;
					}

					&.confirm {
						color: #1890ff;
						font-weight: 500;
					}
				}
			}
		}
	}
</style>